/**
 * 分配角色页面JS
 * User: zhaoming
 * Date: 15-8-17
 * Time: 下午5:58
 * To change this template use File | Settings | File Templates.
 */
$(function(){
    //激活左侧导航菜单
    activeNavBar("AUTHORITY_USER_INFO", "AUTHORITY");
    $('#undo_redo').multiselect();
    initRoleData();
    $("#save").click(function(){
        save();
    });
    $("#goBack").click(function(){
        window.location.href = "/web/userInfo/index";
    });
});

/**
 * 初始化角色数据
 */
function initRoleData(){
    $('#undo_redo').empty();
    $('#undo_redo_to').empty();
    jQuery.ajax({
        type: "POST",
        dataType: "json",
        url: "/web/userRoleRelation/getRoles",
        data:{
            userCode: $("#userCode").val()
        },
        success:function (data) {
            for(var i=0; i<data.length; i++){
                if(data[i].flag == false){
                    $("#undo_redo").append("<option value='" + data[i].roleCode + "'>" + data[i].roleName + "【" + data[i].roleCode + "】</option>");
                }else{
                    $("#undo_redo_to").append("<option value='" + data[i].roleCode + "'>" + data[i].roleName + "【" + data[i].roleCode + "】</option>");
                }

            }
        },
        error : function(data){
            Commons.showError("初始化页面数据失败!");
        }
    });
}

/**
 * 保存
 */
function save(){
    var roleCode = "";
    $("#undo_redo_to").find("option").each(function(){
        roleCode = roleCode + "," + $(this).attr("value");
    });
    if(roleCode != null){
        roleCode = roleCode.substring(1);
    }
    Loading.show();
    jQuery.ajax({
        type: "POST",
        dataType: "json",
        url: "/web/userRoleRelation/assignRole",
        data:{
            userCode: $("#userCode").val(),
            roleCode: roleCode

        },
        success:function (data) {
            Loading.hide();
            Commons.showSuccess("数据保存成功!", function(){
                window.location.href = "/web/userInfo/index";
            });
        },
        error : function(data){
            Loading.hide();
            Commons.showError(data.responseText);
        }
    });
}

